﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using FelMaster.Classes;
using Styx.Logic.Combat;
using Styx.Helpers;

namespace FelMaster.ClassButton
{
    public partial class SelectClass : Form
    {
        private static SelectClass instance = new SelectClass();
        private static FelMaster FM = null;

        public static void Display(FelMaster FM)
        {
            SelectClass.FM = FM;
            if (instance == null || instance.IsDisposed)
                instance = new SelectClass();
            if (!instance.Visible)
                instance.Show();
        }

        private List<TalentBase> talentClasses;
        private SelectClass()
        {
            InitializeComponent();

            var talentTypes = GetClasses(typeof(TalentBase));

            talentClasses = new List<TalentBase>();
            foreach (var x in talentTypes)
            {
                var instance = x.GetConstructor(new Type[] { }).Invoke(new object[] { }) as TalentBase;
                if (SpellManager.HasSpell(instance.KeySpell))
                {
                    Logging.WriteDebug("Adding " + instance.Name + ": toon has " + instance.KeySpell);
                    talentClasses.Add(instance);
                }
                else
                {
                    Logging.WriteDebug("Skipping " + instance.Name + ": toon is missing " + instance.KeySpell);
                }
            }

            var Y = 40;
            var X = 25;
            var columnX = 290;
            foreach (var talent in talentClasses)
            {
                var radio = new RadioButton();
                radio.Text = talent.Name;

                var label = new Label();
                label.Text = "";
                try
                {
                    var foo = talent.SingleRotation;
                    label.Text += (label.Text == "" ? "" : ", ") + "Single";
                }
                catch (NotImplementedException) { }
                try
                {
                    var foo = talent.PVERotation;
                    label.Text += (label.Text == "" ? "" : ", ") + "Party, Raid";
                }
                catch (NotImplementedException) { }
                try
                {
                    var foo = talent.PVPRotation;
                    label.Text += (label.Text == "" ? "" : ", ") + "PvP";
                }
                catch (NotImplementedException) { }

                if (label.Text == "")
                {
                    label.Text = "Not supported";
                    label.ForeColor = Color.Red;
                }

                radio.Location = new Point(X, Y);
                radio.Width = columnX - X;
                label.Location = new Point(columnX, Y);
                label.Width = 435 - columnX;
                radio.Height = 20;
                label.Height = 20;

                radio.TextAlign = ContentAlignment.MiddleLeft;
                label.TextAlign = ContentAlignment.MiddleLeft;

                this.Controls.Add(radio);
                this.Controls.Add(label);

                radio.Click += new EventHandler(radio_Click);

                Y += radio.Height;
            }

            this.ClientSize = new Size(435, Y+50);
            this.MaximumSize = this.ClientSize;
            this.MinimumSize = this.ClientSize;
        }

        void radio_Click(object sender, EventArgs e)
        {
            var radio = sender as RadioButton;
            FM.SetTalentTree(talentClasses.First(x => x.Name == radio.Text));
            this.Close();
        }

        /// <summary>
        /// Get list of types in the calling assembly that inherits
        /// from a certain base type.
        /// </summary>
        /// <param name="baseType">The base type to check for.</param>
        /// <returns>A list of types that inherits from baseType.</returns>
        private static List<Type> GetClasses(Type baseType)
        {
            return Assembly.GetCallingAssembly().GetTypes().Where(type => type.IsSubclassOf(baseType) && !type.IsAbstract).ToList();
        }
    }
}
